﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace executeCommands
{
    public partial class Form1 : Form
    {
        BindingList<DataStorage> bindingSource1 = new BindingList<DataStorage>();
        ProcessCommands handleCommands = new ProcessCommands();
        XmlSerializer serializer = new XmlSerializer(typeof(BindingList<DataStorage>));
        StreamWriter errHandle;
        public Form1()
        {
            
            InitializeComponent();
            
            
            
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);

            if (File.Exists("out.xml"))
            {
                File.Copy("out.xml", "out.xml.bak", true);
                using (StreamReader inHandle = new StreamReader("out.xml"))
                {
                    bindingSource1 = (BindingList<DataStorage>)serializer.Deserialize(inHandle);

                }
            }
            setupBindings();
            errHandle = new StreamWriter("output.txt", true);
            //this.FormClosing +=new FormClosingEventHandler(Form1_FormClosing);
            //dataGridView1.MouseDoubleClick += new MouseEventHandler(dataGridView1_MouseDoubleClick);
            
            //this.Controls.Add(this.dataGridView1);
            //this.Refresh();
            //this.KeyDown += new KeyEventHandler(Form1_KeyDown);
            //dataGridView1.KeyDown += new KeyEventHandler(Form1_KeyDown);
        }

        void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.I){
                bindingSource1.Insert(dataGridView1.CurrentCell.RowIndex,new DataStorage());
            }
            
        }

 

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            using(StreamWriter outHandle = new StreamWriter("out.xml")){
                serializer.Serialize(outHandle, bindingSource1);
            }

            ((IDisposable)errHandle).Dispose();

            

            
        }

        ~Form1()
        {
            ((IDisposable)errHandle).Dispose();
        }
        void setupBindings()
        {    //bindingSource1.Add(new DataStorage());
             //bindingSource1.Add(new DataStorage());
             //bindingSource1.Add(new DataStorage());

            
            
            
            
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.AutoSize = false;
            dataGridView1.DataSource = bindingSource1;

            DataGridViewColumn column = new DataGridViewCheckBoxColumn();
            column.Width = 35;
            dataGridView1.Columns.Add(column);

            //column = new DataGridViewTextBoxColumn();
            //column.DataPropertyName = "command";
            //column.Name = "Commands";

            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
            combo.DataSource = Enum.GetValues(typeof(DataStorage.commandEnum));
            //combo.ValueType = typeof(DataStorage.commandEnum);
            combo.DataPropertyName = "command";
            combo.Name = "Commands";
            //combo.ValueMember = 
            //return combo;
            combo.Width = 71;
            
            
            
            dataGridView1.Columns.Add(combo);

            column = new DataGridViewTextBoxColumn();
            column.DataPropertyName = "arguments";
            column.Name = "Arguments";
            column.Width = 315;
            dataGridView1.Columns.Add(column);

            column = new DataGridViewTextBoxColumn();
            column.DataPropertyName = "comments";
            column.Name = "Comments";
            column.Width = 265;
            dataGridView1.Columns.Add(column);

            

            //bindingSource1.Add(new DataStorage());
            //bindingSource1.Add(new DataStorage());
            //bindingSource1.Add(new DataStorage());

            //dataGridView1.Update();
            column = new DataGridViewCheckBoxColumn();
            column.Width = 50;
            column.DataPropertyName = "success";
            column.Name = "Sucess";
            dataGridView1.Columns.Add(column);
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            
                HandleCommands(0);
            

        }

        private void HandleCommands( int from)
        {
            for (int i = from; i < dataGridView1.RowCount; i++)
            {
                dataGridView1[0, i].Value = false;
                dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
            }

            for (int i = from; i < bindingSource1.Count; i++)
            {
                if ((i - 2) >= 0)
                    dataGridView1.FirstDisplayedScrollingRowIndex = i - 2;
                executeSingleCommand(i);

            }
        }

        private void executeSingleCommand(int i)
        {
            if (bindingSource1[i].command == DataStorage.commandEnum.wait)
            {
                handleCommands.wait();
            }
            else if (bindingSource1[i].command == DataStorage.commandEnum.picture)
            {
                handleCommands.takePicture(bindingSource1[i].arguments);
            }
            else if (bindingSource1[i].command == DataStorage.commandEnum.execute)
            {
                handleCommands.executeCommand(bindingSource1[i].arguments + "{ENTER}");
            }
            else if (bindingSource1[i].command == DataStorage.commandEnum.display)
            {
                if (!handleCommands.displayMesageBox(bindingSource1[i].arguments))
                {
                    errHandle.WriteLine(bindingSource1[i].arguments + " : Step " + i.ToString() + " Failed");
                    //foreach (DataGridViewCell item in dataGridView1.Rows[i].Cells)
                    //{
                    //    item.Style.BackColor = Color.Red;
                    //    bindingSource1[i].success = false;
                    //    //   dataGridView1[1, i].Style.BackColor = Color.Red;
                    //}

                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }
            }
            else if (bindingSource1[i].command == DataStorage.commandEnum.executeRaw)
            {
                handleCommands.executeCommand(bindingSource1[i].arguments);
            }
            else
            {
                MessageBox.Show("Unsupported Command");
            }
            dataGridView1[0, i].Value = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bindingSource1.Insert(dataGridView1.CurrentCell.RowIndex+1, new DataStorage());
        }

        private void button3_Click(object sender, EventArgs e)
        {
     
                HandleCommands(dataGridView1.CurrentCell.RowIndex);
     
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Xml Files (*.xml) | *.xml";
            save.ShowDialog();
            string fileLoc = save.FileName;

            if (!save.CheckFileExists)
            {
                using (StreamWriter outHandle = new StreamWriter(fileLoc))
                {
                    serializer.Serialize(outHandle, bindingSource1);
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Xml Files (*.xml) | *.xml";
            open.ShowDialog();
            //string fileLoc = open.FileName;
            if (open.CheckFileExists)
            {
                using (StreamReader inHandle = new StreamReader(open.OpenFile()))
                {
                    bindingSource1 = (BindingList<DataStorage>)serializer.Deserialize(inHandle);

                }

                
            }

            //bindingSource1.Add(new DataStorage());
           
            //bindingSource1.Remove(bindingSource1.Last());

            dataGridView1.DataSource = bindingSource1;
            dataGridView1.Refresh();
            //this.Refresh();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            executeSingleCommand(dataGridView1.CurrentCell.RowIndex);
            
        }
    }
}
